package org.onion.pattern.event.springboot.rocketmq.topic;


import org.apache.commons.lang3.StringUtils;
import org.onion.pattern.event.springboot.rocketmq.annotation.RocketMQTag;
import org.springframework.core.annotation.AnnotationUtils;

public final class RocketMQTagUtils {

    private RocketMQTagUtils() {
    }

    /**
     * 参考文档
     * https://rocketmq.apache.org/zh/docs/featureBehavior/07messagefilter/
     */
    public static String getTag(Class<?> msgClass) {
        RocketMQTag rocketMQTag = AnnotationUtils.findAnnotation(msgClass, RocketMQTag.class);
        if (rocketMQTag == null) {
            return "";
        }
        if (StringUtils.isNotBlank(rocketMQTag.value())) {
            return rocketMQTag.value();
        }

        String className = StringUtils.replace(msgClass.getName(), ".", "-");
        className = StringUtils.replace(className, "$", "-");
        //Tag使用可见字符，建议长度不超过128字符。
        if (className.length() > 128) {
            int redundantLen = className.length() - 128;
            className = StringUtils.substring(className, redundantLen, className.length());
        }
        return className;
    }


}
